using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using GameFramework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Xna.Framework.GamerServices;

namespace GameForKids
{
    public enum VIEWSTATE
    {
        effectIN,
        effectOUT,
        ready,
        exit
    }

    public enum ICONSETTINGSTATE
    {
        nomal,
        cuonra,
        cuonvo,
        ready
    }

    class MainMenuScreen : GameScreen
    {
        //scollview la list cac icon se hien
        ScrollView _scrollView;
        //action thoat khoi man hinh
        InputAction _quit_action;
        // quan ly cac frame hinh
        List<FrameModule> listFrameModule;
        // quan ly cac ten game
        List<String> listIcon;
        // thanh phan cua 1 icon
        FrameModule frameModule;

        //2 button hien thi trang thai
        FrameModule checkFrame;
        FrameModule uncheckFrame;

        //background
        Texture2D BG;
        Texture2D title;
        Rectangle titleRec;
        int start_pos = 0;
        public static int moveBackground = 0;

        // dam may
        Texture2D cloud1;
        int cloud1_pos_x = 150;

        Texture2D cloud2;
        int cloud2_pos_x = 530;

        // con chim
        Texture2D birdSheet;
        Animation birdAnimation;
        int bird_pos_x = -100;
        // mat troi
        Texture2D thesun;
        float sunRotate;


        SpriteFont font;
        Texture2D pixel;
        int alpha = 250;
        public static VIEWSTATE viewState;


        // phan cai dat
        ICONSETTINGSTATE settingstate;
        Animation IconSettingAnimation;
        Vector2 postOfIconSetting;

        Texture2D crollBG;
        Rectangle rectOfCroll;
        Boolean dacham = false;

        Button LangVN;
        Button LangEN;
        Button SoundBT;
        Button InfoBT;

        SoundEffect soundButtonpress;

        int gameDaChon = 0;

        // nhac nen
        Song background_song;

        public MainMenuScreen()
        {
            EnabledGestures = GestureType.Tap | GestureType.HorizontalDrag | GestureType.DragComplete | GestureType.Flick;
        
            // dung de load hinh
            listIcon = new List<string> { 
                "iconMemory",
                                                        "iconAlphabetMemory",
                                                        "iconNumbersMemory",
                                                        "iconSoundMemory",
                                                        "iconFishtank",
                                                        "iconFindCharacters",
                                                        "iconPuzzle",
                                                        "iconFindSound",
                                                        "iconPackId",
                                                        "iconBilliard",
                                                        "iconDraw",
            "iconMemoryVN",
                                                        "iconAlphabetMemoryVN",
                                                        "iconNumbersMemoryVN",
                                                        "iconSoundMemoryVN",
                                                        "iconFishtankVN",
                                                        "iconFindCharactersVN",
                                                        "iconPuzzleVN",
                                                        "iconFindSoundVN",
                                                        "iconPackId",
                                                        "iconBilliard",
                                                        "iconDrawVN"};
       
            TransitionOnTime = TimeSpan.FromSeconds(0);
        }

        public override void Activate(bool instancePreserved)
        {
            // trang thai 
            viewState = VIEWSTATE.effectIN;
            settingstate = ICONSETTINGSTATE.nomal; 
            //nhac nen
            background_song = ScreenManager.Game.Content.Load<Song>("MainMenu/Sounds/music_of_main_menu");
            // load cac texture
            pixel = new Texture2D(ScreenManager.GraphicsDevice, 1, 1);
            pixel.SetData(new[] { Color.Black });
            BG = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/menuBackground");
            thesun = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/the_sun");
            cloud1 = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/cloud1");
            cloud2 = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/cloud2");
            title = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/title");
            font = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/LoadFont");
            // cai dat chim
            birdSheet = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/bird_sheet");
            birdAnimation = new Animation(birdSheet, new Point(91, 84), new Point(5, 3));
            birdAnimation.PlayFromFrameIndex(0);
            birdAnimation.Active();

            // cai dat setting
            IconSettingAnimation = new Animation(ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/icon_setting_sheet"),
                new Point(72, 72), new Point(5, 1));
            IconSettingAnimation.PlayFromFrameIndex(0);
            IconSettingAnimation.Deactive();
            postOfIconSetting = new Vector2(715,400);

            // hinh nen thanh cuon
            crollBG = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/croll_setting");
            rectOfCroll = new Rectangle(0, 0, 0, crollBG.Height);

            // button cua En
            LangEN = new Button(ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/EN_on"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/EN_off"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/EN_off"));
            LangEN.Location = new Point(0, 0);
            LangEN.Clicked += LangEN_Clicked;

            //button cua VN
            LangVN = new Button(ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/VN_on"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/VN_off"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/VN_off"));
            LangVN.Location = new Point(0, 0);
            LangVN.Clicked += LangVN_Clicked;
            // button sound
            SoundBT = new Button(ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/icon_sound_on"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/icon_sound_off"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/icon_sound_off"));
            SoundBT.Location = new Point(0, 0);
            SoundBT.Clicked += SoundBT_Clicked;
            //button info
            InfoBT = new Button(ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/info_button"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/info_button_press"),
                ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/info_button_press"));
            InfoBT.Location = new Point(13, 400);
            InfoBT.Clicked += InfoBT_Clicked;
            //
            soundButtonpress = ScreenManager.Game.Content.Load<SoundEffect>("Login/Sounds/button_click");
            //kiem tra cac button
            if (GameForKids.ISMUSIC == false)
                SoundBT.Hold = true;
            if (GameForKids.language == GameForKids.ANH)
            {
                LangVN.Hold = true;
            }
            else
            {
                LangEN.Hold = true;
            }


            titleRec = new Rectangle((GameForKids.WIDTH-title.Width)/2,10,title.Width,title.Height);

            TaoLaiMenu();

            _quit_action = new InputAction(new Buttons[] { Buttons.Back }, null, true);


            if (GameForKids.ISMUSIC)
            {
                if (MediaPlayer.GameHasControl && MediaPlayer.State != MediaState.Playing)
                {
                    MediaPlayer.IsRepeating = true;
                    MediaPlayer.Play(background_song);
                }
                else
                {
                    Guide.BeginShowMessageBox(
                    "Use game music?",
                    "If use game music  you can't listen your music from phone while playing !",
                    new[] { "Yes", "No" },
                    0,
                    MessageBoxIcon.None,
                    result =>
                    {

                        int? choice = Guide.EndShowMessageBox(result);

                        if (choice.HasValue && choice.Value == 0)
                        {
                            MediaPlayer.Stop();
                            MediaPlayer.IsRepeating = true;
                            MediaPlayer.Play(background_song);
                        }
                        else
                        {
                            GameForKids.ISMUSIC = false;
                            SoundBT.Hold = true;
                        }
                    },
                    null);
                }
            }

            base.Activate(instancePreserved);
        }

        public void TaoLaiMenu()
        {
            // tao mot frame , add vao list
            _scrollView = new ScrollView(ScreenManager.Game);
            listFrameModule = null;
            listFrameModule = new List<FrameModule>();
            checkFrame = new FrameModule();
            uncheckFrame = new FrameModule();

            for (int i = 0; i < 11; i++)
            {
                frameModule = new FrameModule();
                if(GameForKids.language == GameForKids.ANH)
                    frameModule.SourceTexture = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/" + listIcon[i]);
                else
                    frameModule.SourceTexture = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/" + listIcon[i + 11]);
                frameModule.NumOfModuleFrame = 1;
                frameModule.SourceRectangle = new List<Rectangle> { new Rectangle(0, 0, 350, 250) };
                frameModule.Offsets = new List<Vector2> { Vector2.Zero };
                frameModule.Origin = new List<Vector2> { new Vector2(175, 125) };
                frameModule.SpriteBatch = ScreenManager.SpriteBatch;
                listFrameModule.Add(frameModule);
            }

            for (int i = 0; i < 11; i++)
            {
                _scrollView.AddItem(new StaticViewItem(ScreenManager.Game, listFrameModule[i], listIcon[i]));
            }

            // khoang cach cac item menu
            _scrollView.SetUpItemsPosition(400);

            checkFrame.SourceTexture = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/check");
            checkFrame.NumOfModuleFrame = 1;
            checkFrame.SourceRectangle = new List<Rectangle> { new Rectangle(0, 0, 10, 10) };
            checkFrame.Offsets = new List<Vector2> { Vector2.Zero };
            checkFrame.Origin = new List<Vector2> { new Vector2(10, 10) };
            checkFrame.SpriteBatch = ScreenManager.SpriteBatch;

            uncheckFrame.SourceTexture = ScreenManager.Game.Content.Load<Texture2D>("MainMenu/Images/uncheck");
            uncheckFrame.NumOfModuleFrame = 1;
            uncheckFrame.SourceRectangle = new List<Rectangle> { new Rectangle(0, 0, 10, 10) };
            uncheckFrame.Offsets = new List<Vector2> { Vector2.Zero };
            uncheckFrame.Origin = new List<Vector2> { new Vector2(10, 10) };
            uncheckFrame.SpriteBatch = ScreenManager.SpriteBatch;

            //ve mot chuoi button hien thi trang thai menu
            _scrollView.EnableDisplayItemNumber(new Vector2(400, 470), 20, uncheckFrame, checkFrame);
            //xu ly su kien khi chon 1 game
            _scrollView.ItemSelected += _scrollView_ItemSelected;
        }

        void InfoBT_Clicked(object sender, EventArgs e)
        {
            if(GameForKids.ISMUSIC)
                soundButtonpress.Play();
            if (viewState == VIEWSTATE.ready)
            {
                moveBackground = 0;
                GameForKids.gameState = GameState.Info;
                ScreenManager.AddScreen(new InfoScreen(), null);
            }
            settingstate = ICONSETTINGSTATE.cuonvo;
            dacham = true;
        }

        void SoundBT_Clicked(object sender, EventArgs e)
        {
            
            if (GameForKids.ISMUSIC)
            {
                soundButtonpress.Play();
                MediaPlayer.Stop();
                GameForKids.ISMUSIC = false;
                SoundBT.Hold = true;
            }
            else
            {
                MediaPlayer.Stop();
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(background_song);
                GameForKids.ISMUSIC = true;
                SoundBT.Hold = false;
            }
            SaveSetting();
            settingstate = ICONSETTINGSTATE.cuonvo;
            dacham = true;
        }

        void LangVN_Clicked(object sender, EventArgs e)
        {
            moveBackground = 0;
            if(GameForKids.ISMUSIC)
            soundButtonpress.Play();
            if (LangVN.Hold == true)
            {
                
                LangVN.Hold = false;
                LangEN.Hold = true;
                GameForKids.language = GameForKids.VIET;
                TaoLaiMenu();
            }
            SaveSetting();
            settingstate = ICONSETTINGSTATE.cuonvo;
            dacham = true;
        }

        void LangEN_Clicked(object sender, EventArgs e)
        {
            moveBackground = 0;
            if(GameForKids.ISMUSIC)
            soundButtonpress.Play();
            if (LangEN.Hold == true)
            {
                LangEN.Hold = false;
                LangVN.Hold = true;
                GameForKids.language = GameForKids.ANH;
                TaoLaiMenu();
            }
            SaveSetting();
            settingstate = ICONSETTINGSTATE.cuonvo;
            dacham = true;
        }



        // luu lai setting
        public void SaveSetting()
        {
            using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = isolatedStorageFile.CreateFile(GameForKids.NameOfFileSetting))
                {
                    using (StreamWriter writer = new StreamWriter(isfs))
                    {
                            writer.WriteLine(GameForKids.language.ToString());
                            if (GameForKids.ISMUSIC == true)
                                writer.WriteLine(GameForKids.ON);
                            else
                                writer.WriteLine(GameForKids.OFF);
                            writer.Close();
                    }
                }
            }
        }


        void _scrollView_ItemSelected(object sender, EventArgs e)
        {
            StaticViewItem selectedItem = (StaticViewItem)sender;
            string tag = selectedItem.Tag as string;
            switch (tag)
            {
                case "iconMemory":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 1;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconAlphabetMemory":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 2;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconNumbersMemory":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 3;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconSoundMemory":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 4;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconFishtank":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 5;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconFindCharacters":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 6;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconPuzzle":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 7;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconFindSound":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 8;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconPong":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 9;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconPackId":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 10;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconBilliard":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 11;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                case "iconDraw":
                    {
                        viewState = VIEWSTATE.effectOUT;
                        gameDaChon = 12;
                        settingstate = ICONSETTINGSTATE.cuonvo;
                        dacham = true;
                    }
                    break;
                default:break;
            }
        }

        public override void Unload()
        {
            _scrollView = null;
            listFrameModule = null;
            listIcon = null;
            base.Unload();
        }

        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            if(viewState == VIEWSTATE.ready)
                _scrollView.HandleInput(gameTime, input);

            PlayerIndex player;
            if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
            {
                viewState = VIEWSTATE.effectOUT;

            }
             TouchCollection touches = input.TouchState;
             if (touches.Count > 0 && viewState == VIEWSTATE.ready)
             {
                 TouchLocation touch = touches[0];
                 {
                     InfoBT.TouchEvent(touch);
                     if ( settingstate == ICONSETTINGSTATE.ready)
                     {
                         LangVN.TouchEvent(touch);
                         LangEN.TouchEvent(touch);
                         SoundBT.TouchEvent(touch);
                         
                     }
                     if (touch.Position.X > postOfIconSetting.X &&
                         touch.Position.X < postOfIconSetting.X + 72 &&
                         touch.Position.Y > postOfIconSetting.Y &&
                         touch.Position.Y < postOfIconSetting.Y + 72)
                     {
                         if (GameForKids.ISMUSIC)
                             SoundBT.Hold = false;
                         else
                             SoundBT.Hold = true;
                         if ((settingstate == ICONSETTINGSTATE.nomal || settingstate == ICONSETTINGSTATE.ready)&& GameForKids.ISMUSIC)
                             soundButtonpress.Play();
                         IconSettingAnimation.Active();
                         if ((settingstate == ICONSETTINGSTATE.nomal || settingstate == ICONSETTINGSTATE.cuonvo) && !dacham)
                         {
                             settingstate = ICONSETTINGSTATE.cuonra;
                             dacham = true;
                         }
                         else if ((settingstate == ICONSETTINGSTATE.ready || settingstate == ICONSETTINGSTATE.cuonra)&&!dacham)
                         {
                             settingstate = ICONSETTINGSTATE.cuonvo;
                             dacham = true;
                         }
                     }
                 }
             }
            base.HandleInput(gameTime, input);
        }

        private float timedelay=0;
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
         
            float elapsed = (float)gameTime.ElapsedGameTime.Milliseconds;

            //cap nhat menu
            _scrollView.Update(gameTime);

           // cap nhat goc xoay cua mat troi
            sunRotate += (float)Math.PI / 60;
            if (sunRotate >= (float)Math.PI * 2)
                sunRotate = 0;
            // cap nhat vi tri background
            if(start_pos + moveBackground > 0 && start_pos + moveBackground< BG.Width - GameForKids.WIDTH)
            start_pos += moveBackground;
            // cap nhat vi tri dam may
            cloud1_pos_x -= 1;
            cloud2_pos_x -= 2;

            if (cloud1_pos_x < 0 - cloud1.Width)
                cloud1_pos_x = 800;
            if (cloud2_pos_x < 0 - cloud2.Width)
                cloud2_pos_x = 800;
            // cap nhat chim
            birdAnimation.Update();

            // cap nhat nut setting
            IconSettingAnimation.Update();
           
            // con chim bay khoi man hinh
            if (bird_pos_x > 800)
            {
                timedelay += elapsed;
            }
            else
            {
                bird_pos_x += 5;
            }
            if (timedelay > 5000)
            {
                bird_pos_x = -100;
                timedelay = 0;
            }

            // cap nhat hieu ung
            if (alpha > 20 && viewState == VIEWSTATE.effectIN)
                alpha -= 20;
            else if (alpha <= 20 && viewState == VIEWSTATE.effectIN)
            {
                alpha = 0;
                viewState = VIEWSTATE.ready;
               
            }

            if (alpha < 250 && viewState == VIEWSTATE.effectOUT)
                alpha += 20;
            else if (alpha >= 250 && viewState == VIEWSTATE.effectOUT)
            {
                alpha = 255;
                viewState = VIEWSTATE.exit;
            }

            if (viewState == VIEWSTATE.exit)
            {
                if (gameDaChon == 0)
                {
                    moveBackground = 0;
                    if(GameForKids.ISMUSIC)
                    MediaPlayer.Stop();
                    ExitScreen();
                    Unload();
                    ScreenManager.AddScreen(new LoginScreen(), null);
                }
                else
                {
                    moveBackground = 0;
                    switch (gameDaChon)
                    {
                        case 1:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.Memory;             
                                ScreenManager.AddScreen(new MemoryGame(), null);
                            }; break;
                        case 2:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.AlphabetMemory;
                                ScreenManager.AddScreen(new MemoryGame(), null);
                            };break;
                        case 3:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.NumbersMemory;
                                ScreenManager.AddScreen(new MemoryGame(), null);
                            };break;
                        case 4:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.SoundMemory;
                                ScreenManager.AddScreen(new MemoryGame(), null);
                            };break;
                        case 5:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.Fishtank;
                                ScreenManager.AddScreen(new FishGame(), null);
                            };break;
                        case 6:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.FindCharacters;
                                ScreenManager.AddScreen(new FindCharactersGame(), null);
                            };break;
                        case 7:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.Puzzle;
                                ScreenManager.AddScreen(new PuzzleGame(), null);
                            };break;
                        case 8:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.FindSound;
                                ScreenManager.AddScreen(new FindSoundGame(), null);
                            };break;
                        case 9:
                            {
                             /*   viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.Pong;
                                ScreenManager.AddScreen(new PingPongGame(), null);*/
                            };break;
                        case 10:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.PackId;
                                ScreenManager.AddScreen(new PacManGame(), null);
                            };break;
                        case 11:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.Billiard;
                                ScreenManager.AddScreen(new BilliardGame(), null);
                            };break;
                        case 12:
                            {
                                viewState = VIEWSTATE.ready;
                                gameDaChon = 0;
                                GameForKids.gameState = GameState.DrawNumbers;
                                ScreenManager.AddScreen(new DrawGame(), null);
                            }; break;
                    }
                }
            }

            // cuon cai setting
            if (rectOfCroll.Width < crollBG.Width - 10 && settingstate == ICONSETTINGSTATE.cuonra)
            {
                rectOfCroll.Width += 30;
            }
            else if (rectOfCroll.Width >= crollBG.Width - 10 && settingstate == ICONSETTINGSTATE.cuonra)
            {
                settingstate = ICONSETTINGSTATE.ready;
                IconSettingAnimation.Deactive();
                dacham = false;
            }
            if (rectOfCroll.Width > 0 && settingstate == ICONSETTINGSTATE.cuonvo)
            {
                rectOfCroll.Width -= 30;
            }
            else if (rectOfCroll.Width <= 0 && settingstate == ICONSETTINGSTATE.cuonvo)
            {
                settingstate = ICONSETTINGSTATE.nomal;
                IconSettingAnimation.Deactive();
                dacham = false;
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SpriteBatch spritebatch = ScreenManager.SpriteBatch;
            spritebatch.Begin();
         // ve hinh nen
            spritebatch.Draw(BG, Vector2.Zero,
                new Rectangle(start_pos, 0, GameForKids.WIDTH,GameForKids.HEIGHT ),
                Color.White,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0f);

            // ve mat troi
            spritebatch.Draw(thesun,
                  new Vector2(140, 100),
                  null,
                  Color.White,
                  sunRotate,
                  new Vector2(thesun.Width / 2, thesun.Height / 2),
                  1.0f,
                  SpriteEffects.None,
                  0f);

            // ve chim
            birdAnimation.Draw(spritebatch, new Vector2(bird_pos_x, 10), 1.0f, SpriteEffects.None, 255);

            // ve 2 cuc may
            spritebatch.Draw(cloud1, new Rectangle(cloud1_pos_x, 0, cloud1.Width, cloud1.Height), Color.White);
            spritebatch.Draw(cloud2, new Rectangle(cloud2_pos_x, 70, cloud2.Width, cloud2.Height), Color.White);

            // ve tieu de
            spritebatch.Draw(title, titleRec, Color.White);
            

            //xem RAM
          /*  var appCurrentMemoryUsage = DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
            var appPeakMemoryUsage = DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");
            spritebatch.DrawString(font, "Current RAM: " + appCurrentMemoryUsage.ToString(), new Vector2(10, 10), Color.White);
            spritebatch.DrawString(font, "Total RAM: " +appPeakMemoryUsage.ToString(), new Vector2(10, 50), Color.White);
            */
            spritebatch.End();

            _scrollView.Draw(gameTime);

            spritebatch.Begin();
            // ve info button
            InfoBT.Draw(spritebatch);
            //ve nen croll setting
            spritebatch.Draw(crollBG,
                new Vector2(postOfIconSetting.X - rectOfCroll.Width + 36, postOfIconSetting.Y),
                rectOfCroll,
                Color.White,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0);
            // ve ba button 
            if (rectOfCroll.Width > 49)
            {
                SoundBT.Location = new Point((int)(postOfIconSetting.X - rectOfCroll.Width + 49),
                   (int) (postOfIconSetting.Y+(crollBG.Height - SoundBT.height)/2));
                SoundBT.Draw(spritebatch);
            }
            if (rectOfCroll.Width > 108)
            {
                LangEN.Location = new Point((int)(postOfIconSetting.X - rectOfCroll.Width + 119),
                   (int)(postOfIconSetting.Y + (crollBG.Height - LangEN.height) / 2));
                LangEN.Draw(spritebatch);
            }
            if (rectOfCroll.Width > 178)
            {
                LangVN.Location = new Point((int)(postOfIconSetting.X - rectOfCroll.Width + 189),
                   (int)(postOfIconSetting.Y + (crollBG.Height - LangVN.height) / 2));
                LangVN.Draw(spritebatch);
            }


            // ve icon setting
            IconSettingAnimation.Draw(spritebatch, postOfIconSetting, 1.0f, SpriteEffects.None, 255);

            if(viewState !=VIEWSTATE.ready)
            spritebatch.Draw(pixel, new Rectangle(0, 0, GameForKids.WIDTH, GameForKids.HEIGHT), new Color(255, 255,255,alpha));

            spritebatch.End();

            base.Draw(gameTime);
        }
    }
}
